package com.ken.business.demo.controller;

import com.ken.business.demo.service.StudentService;
import com.ken.business.protocol.input.StudentInput;
import com.ken.common.core.result.R;
import com.ken.common.core.utils.RUtils;
import com.ken.common.web.apiversion.annotation.ApiVersion;
import com.ken.data.entity.demo.Student;
import com.ken.feign.demo2.Demo2Feign;
import feign.Client;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.validation.Valid;
import java.util.List;

@RestController
@Slf4j
@RefreshScope
public class DemoController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private Demo2Feign demo2Feign;

    @Autowired
    private Client client;

    @PostConstruct
    public void init(){
        System.out.println("feign的请求对象：" + ((LoadBalancerFeignClient)client).getDelegate());
    }

    @PostMapping("/insertStu")
    public R insertStudent(@Valid StudentInput input){
        System.out.println("学生信息：" + input);

        Student student = new Student();
        BeanUtils.copyProperties(input, student);
        //保存学生信息
        boolean result = studentService.save(student);
        System.out.println("保存学生的结果：" + result);

        return RUtils.createSucc(input);
    }

    @GetMapping("/stuList")
    public R selectStudent(){
        List<Student> list = studentService.list();
        return RUtils.createSucc(list);
    }

    @GetMapping("/test")
    @ApiVersion(1.0)
    public R<String> test(){
        log.debug("[demo1-test] - demo1的测试接口被调用！");
        //调用demo2的test接口
        R result = demo2Feign.test();
        return RUtils.createSucc("demo1-test, " + result.getData());
    }

//    @GetMapping("/test")
//    @ApiVersion(2.0)
//    public R<String> test2(){
//        log.info("[My-Test] - 我的日志222");
//        return RUtils.createSucc("msg2222");
//    }
//
//    @GetMapping("/test")
//    @ApiVersion(3.0)
//    public R<String> test3(){
//        log.info("[My-Test] - 我的日志333");
//        return RUtils.createSucc("msg3333");
//    }
}
